package com.tianqu.tms.entity;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.TmsEntity;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.UserAuthDao;
import com.tianqu.tms.dto.UserAuthDto;

public class UserAuth extends TmsEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Integer id;
	// 用户ID
	private Integer userId;
	// rolID
	private Integer rId;
	
	
	public static UserAuth getInstance() {
		UserAuth UrolId = new UserAuth();
		UrolId.setExisting(false);
		return UrolId;
	}

	/**
	 * 获得用户Rolid
	 * 
	 * @return
	 * @throws TmsDaoException
	 */
	public List<UserAuth> getAuth(int id) throws TmsDaoException {

		List<UserAuth> list = new ArrayList<UserAuth>();
		UserAuthDao dao = (UserAuthDao) TmsDao
				.getInstance(TmsDao.TMS_USER_AUTH);
		list = entity(dao.getRolId(id));
		return list;

	}


	
	/**
	 * dto entity转化
	 * @param list
	 * @return
	 */
	private List<UserAuth> entity(List<UserAuthDto> list) {
		if (list.size() > 0) {
			List<UserAuth> entity = new ArrayList<UserAuth>();
			for (UserAuthDto dto : list) {
				UserAuth auth = new UserAuth();
				auth.setId(dto.getId());
				auth.setrId(dto.getrId());
				auth.setUserId(dto.getUserId());
				entity.add(auth);
			}
			return entity;
		}
		return null;
	}
	/**
	 * 
	 * 添加权限信息
	 * @return
	 * @throws TmsDaoException 
	 */
	public void insertUserAuthInfo(int userId,int ucId) throws TmsDaoException
	{
		UserAuthDao dao = (UserAuthDao) TmsDao.getInstance(TmsDao.TMS_USER_AUTH);
		dao.insertUserAuthInfo(userId,ucId);
	}

	/**
	 * 
	 * 根据USer表主键ID删除role信息
	 * 
	 * @return
	 * @throws TmsDaoException 
	 */
	public void delUserAuthInfo(int userId) throws TmsDaoException
	{
		UserAuthDao dao = (UserAuthDao) TmsDao.getInstance(TmsDao.TMS_USER_AUTH);
		dao.delUserAuthInfo(userId);
	}
	
	
	/**
	 * 删除权限相同
	 * @param rId
	 * @throws TmsDaoException 
	 */
	public void deleteAuth(int rId) throws TmsDaoException{
		UserAuthDao dao = (UserAuthDao) TmsDao.getInstance(TmsDao.TMS_USER_AUTH);
		dao.deleteAuth(rId);
	}
	public boolean getAuthInfoByRole(int userid,int role) throws TmsDaoException
	{
		UserAuthDao dao = (UserAuthDao) TmsDao.getInstance(TmsDao.TMS_USER_AUTH);
		UserAuthDto dto = dao.getAuthInfoByRole(userid,role);
		if(dto!=null)
		{
			return true;
		}
		return false;
	}
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public Integer getrId() {
		return rId;
	}

	public void setrId(Integer rId) {
		this.rId = rId;
	}

	@Override
	public void save() throws TmsDaoException, TmsException {
	}

	
	
}
